package com.diandian.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.diandian.util.StringUtil;
import com.diandian.util.VerbalAssociation;

/**
 * @description VerbalAssociationController 错词库
 * @author TDW
 * @date 2018年8月7日
 *
 */
@Controller
@RequestMapping("/verbalAss")
public class VerbalAssociationController {
	
	
	
	@RequestMapping("/goSddVerbaAssociation")
	public String goAddVerbaAssociation(HttpServletRequest req) {
		req.setAttribute("verbalAssociation", VerbalAssociation.correctWords);
		req.setAttribute("firstCut", VerbalAssociation.filterCutWords);
		return "/WEB-INF/views/verbal_association.jsp";

	}
	
	
	@RequestMapping("/add")
	public String add(HttpServletRequest req,String words) {
		System.out.println("xxxxxxxx");
		if(StringUtil.isBlank(words)){
			return "/WEB-INF/views/verbal_association.jsp";
		}
		HashMap<String, String> map = new HashMap<String, String>();
		String[] wordStrs = words.split(";");
		String errWord = "";
		String correctWord = "";
		for(String word : wordStrs){
			String[] wordArr = word.split("#");
			if(wordArr.length != 2){
				req.setAttribute("pass", false);
				return "/WEB-INF/views/verbal_association.jsp"; 
			}
			errWord=wordArr[0];
			correctWord=wordArr[1];
			map.put(errWord, correctWord);
		}
		VerbalAssociation.addWords(map);
		
		req.setAttribute("verbalAssociation", VerbalAssociation.correctWords);
		req.setAttribute("firstCut", VerbalAssociation.filterCutWords);
		
		
		return "/WEB-INF/views/verbal_association.jsp";

	}

	@RequestMapping("/addFirstCut")
	public String addFirstCut(HttpServletRequest req,String words) {
		if(StringUtil.isBlank(words)){
			return "/WEB-INF/views/verbal_association.jsp";
		}
		HashSet<String> set = new HashSet<String>();
		String[] wordStrs = words.split("#");
		for(String word : wordStrs){
			if(StringUtil.isNoneBlank(word)){
				set.add(word);
			}
		}
		VerbalAssociation.addFirstCutWords(set);
		req.setAttribute("verbalAssociation", VerbalAssociation.correctWords);
		req.setAttribute("firstCut", VerbalAssociation.filterCutWords);
		return "/WEB-INF/views/verbal_association.jsp";

	}


	
	
	
}
